﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using BSDS.Core.Data;
using BSDS.Core.Data.Local;

namespace BSDS.Core.Model.Services.Uploading.DataProviders
{
    public class XmlDataProvider : DataProviderBase, IDataProvider
    {
        private readonly string filePath;
        private readonly string entityTypeName;


        public XmlDataProvider(string filePath, string entityTypeName, DataFormat dataFormat)
            : base(dataFormat)
        {
            this.filePath = filePath;
            this.entityTypeName = entityTypeName;
        }

        public void Initialize()
        {
            const string xs = "http://www.w3.org/2001/XMLSchema";

            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var document = XDocument.Load(fileStream);

                var nameAttribute = document.Descendants(XName.Get("element", xs)).Attributes(XName.Get("name")).First();

                nameAttribute.Value = entityTypeName;

                document.Save(MemoryStream);
            }
        }
    }
}